Lex file


%{
 #include"y.tab.h"
%}
alpha [A-Za-z]
digit [0-9]
%%


[ \t\n]
select            return SELECT;
distinct         return DISTINCT;
from             return FROM;
where           return WHERE;
like               return LIKE;
desc              return DESC;
asc                return ASC;
"group by"    return GROUP;
having          return HAVING;
"order by"    return ORDER;
or                 return OR;
and               return AND;
{digit}+       return NUM;
{alpha}({alpha}|{digit})* return ID;
"<="             return LE;
">="             return GE;
"=="             return EQ;
"!="              return NE;
.                   return yytext[0];

%%
void yyerror(char *str)
{
printf("\n Invalid Character…");
}



 



yacc File


%{
#include <stdio.h>
#include <stdlib.h>
%}
%token ID NUM SELECT DISTINCT FROM WHERE LE GE EQ NE OR AND LIKE GROUP HAVING ORDER ASC DESC
%right '='
%left AND OR
%left '<' '>' LE GE EQ NE

%%

    S         : ST1';' {printf("INPUT ACCEPTED.... \n\t\t^^^^^^^^^^^________^^^^^^^^^^^^^^^^^\n");exit(0);};
    ST1     : SELECT attributeList FROM tableList ST2
               | SELECT DISTINCT attributeList FROM tableList ST2
               ;
    ST2     : WHERE COND ST3
               | ST3
               ;
    ST3     : GROUP attributeList ST4
               | ST4
               ;
    ST4     : HAVING COND ST5
               | ST5
               ;
    ST5     : ORDER attributeList ST6
               |
               ;
    ST6     : DESC
               | ASC
               |
               ;
  attributeList :     ID','attributeList
               | '*'
               | ID
               ;
 tableList    : ID',' tableList
               | ID
               ;
    COND    : COND OR COND
               | COND AND COND
               | E
               ;
    E         : F '=' F
               | F '<' F
               | F '>' F 
               | F LE F
               | F GE F
               | F EQ F
               | F NE F
               | F OR F
               | F AND F
               | F LIKE F
               ;
    F         : ID
               | NUM 
               ;
%%
#include"lex.yy.c"
#include<ctype.h>
main()
{
    printf("Enter the query:");
    yyparse();
}       




mohit@mohit-PC ~/Desktop/NetBeans IDE 8.0/sql $ lex  sql.l
mohit@mohit-PC ~/Desktop/NetBeans IDE 8.0/sql $ yacc sql.y
sql.y:57 parser name defined to default :"parse"
mohit@mohit-PC ~/Desktop/NetBeans IDE 8.0/sql $ gcc y.tab.c -ll -ly
mohit@mohit-PC ~/Desktop/NetBeans IDE 8.0/sql $ ./a.out 
Enter the query:select * from employee
;
INPUT ACCEPTED.... 
		^^^^^^^^^^^________^^^^^^^^^^^^^^^^^
mohit@mohit-PC ~/Desktop/NetBeans IDE 8.0/sql $ 

